import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Shapes 1.15

ParaCalSubItem{
    id: root
    title: qsTr("参数设置")
    width: 784
    height: 338
    property color btnColor: "#7080C0"
    property int btnHeight: 50
    property int btnWidth: 125
    signal startLoad
    function isInteger(obj) {
     return (obj | 0) === obj
    }
    onSignalPre: {
    }
    onSignalNext: {
    }
    onSignalOK: {
        if(paraCalLoader.visible===true){
            return
        }
        if(decimalPlaces.currentIndex < 0 || minGraduation.currentIndex < 0){
            paraSaveOKDialog.title=qsTr("有选项未选中！");
            paraSaveOKDialog.open();
            paraSaveOKDialog.accepted.connect(function() {
                paraSaveOKDialog.close()

            })
            return;
        }
        if(!isInteger(Number(maxRange.text))){
            paraSaveOKDialog.title=qsTr("最大量程必须是整数！");
            paraSaveOKDialog.open();
            paraSaveOKDialog.accepted.connect(function() {
                paraSaveOKDialog.close()

            })
            return;
        }
        if(Number(maxRange.text)<minGraduation.currentValue){
            paraSaveOKDialog.title=qsTr("最大量程不能小于最小分度！");
            paraSaveOKDialog.open();
            paraSaveOKDialog.accepted.connect(function() {
                paraSaveOKDialog.close()

            })
            return;
        }

        if(Number(maxRange.text)>minGraduation.currentValue*100000){
            paraSaveOKDialog.title=qsTr("最大量程不能超过最小分度×100000！");
            paraSaveOKDialog.open();
            paraSaveOKDialog.accepted.connect(function() {
                paraSaveOKDialog.close()

            })
            return;
        }
        //normalSettingSaveOK=false;
        paraCalLoader.visible=true
        var result =
        mymodbus.setDecimalPlaces(decimalPlaces.currentValue)
        && mymodbus.setMinGraduation(minGraduation.currentValue)
        && mymodbus.setMaxRange(Number(maxRange.text));
        paraCalLoader.visible=false
        if(result){
            //normalSettingSaveOK=true;

            paraCalView.signalSuccess()
            //normalParaSaveOKDialog.open()
//            normalParaSaveOKDialog.accepted.connect(function() {
//                                    normalParaSaveOKDialog.close()
//                                })
        }
        else
        {
            paraCalView.signalFailed()
//            normalParaSaveOKDialog.open()
//            normalParaSaveOKDialog.accepted.connect(function() {
//                                    normalParaSaveOKDialog.close();
//                                })
        }
    }
    onStartLoad: {
        //paraCalLoader.visible=true
        //unit.currentIndex = unit.find(mymodbus.unit)
        mymodbus.getDecimalPlaces()
        mymodbus.getMinGraduation()
        mymodbus.getMaxRange()

        decimalPlaces.currentIndex = decimalPlaces.find(mymodbus.decimalPlaces);
        minGraduation.currentIndex = minGraduation.find(mymodbus.minGraduation);
        maxRange.text=mymodbus.maxRange
        //unit.currentIndex=unit.find(mymodbus.unit)
        //axisCount.currentIndex=axisCount.find(mymodbus.axisCount)
        //maxRange.text=mymodbus.maxRange
        //mymodbus.decimalPlacesChanged.connect(onDecimalPlacesChanged);
        //mymodbus.minGraduationChanged.connect(onMinGraduationChanged);
    }
    Component.onCompleted: {
        startLoad()

    }
    Rectangle{
        id: mainRec
        anchors.centerIn: parent
        GridLayout{
                        id: normalSettingsLayout
                        rows: 1
                        columns: 6
                        anchors.centerIn: parent
                        rowSpacing: 40
                        Button
                        {
                            Layout.row:0
                            Layout.column:0
                            Layout.preferredWidth: btnWidth
                            Layout.preferredHeight: btnHeight
                            text: qsTr("小数点位数")
                            font.pixelSize: 18
                            background: Rectangle{
                                color: btnColor
                                    }
                            onClicked: {
                                decimalPlaces.popup.open()
                            }
                        }
                        MyComboBox
                        {
                            id:decimalPlaces
                            Layout.row:0
                            Layout.column:1
                            Layout.preferredWidth: btnWidth
                            Layout.preferredHeight: btnHeight
                            model:[0,1,2,3,4]
                            //currentIndex: mymodbus.decimalPlaces
                        }
                            Button
                            {
                                Layout.row:0
                                Layout.column:2
                                Layout.preferredWidth: btnWidth
                                Layout.preferredHeight: btnHeight
                                // x: 152
                                // y: 13
                                text: qsTr("最小分度")
                                font.pixelSize: 18
                                background: Rectangle{
                                    color: btnColor
                                        }
                                onClicked: {
                                    //miniKeyBoard.lineValue = "";
                                    //miniKeyBoard.textInput=oldPwd;
                                    minGraduation.popup.open()
                                }
                            }
                            MyComboBox
                            {
                                id:minGraduation
                                Layout.row:0
                                Layout.column:3
                                Layout.preferredWidth: btnWidth
                                Layout.preferredHeight: btnHeight
                                model:[1, 2, 5, 10, 20, 50]
                                //currentIndex: mymodbus.minGraduation
                            }
                            Button
                            {
                                Layout.row:0
                                Layout.column:4
                                Layout.preferredWidth: btnWidth
                                Layout.preferredHeight: btnHeight
                                // x: 152
                                // y: 13
                                text: qsTr("最大量程")
                                font.pixelSize: 18
                                background: Rectangle{
                                    color: btnColor
                                        }
                                onClicked: {
                                    //miniKeyBoard.lineValue = "";
                                    //miniKeyBoard.textInput=oldPwd;
                                   //rectangle.selectedView=16
                                   enterNumberPanel.lastButtonIndex = 4;
                                   enterNumberPanel.lastViewIndex =8;
                                   enterNumberPanel.minimum=0;
                                   enterNumberPanel.clearText()
                                   //enterNumberPanel.maximum=100000*mymodbus.minGraduation>999999?999999:100000*mymodbus.minGraduation
                                   enterNumberPanel.itemName=qsTr("最大量程")
                                   enterNumberPanel.itemValue=maxRange.text
                                   enterNumberPanel.textInput=maxRange;

                                    enterNumberPanel.visible=true;
                                    enterNumberPanel.lastRec=mainRec
                                    mainRec.visible=false
                                }
                            }
                            Rectangle {
                                id: recMaxRange
                                color: "transparent"
                                border.width: 0
                                border.color: "white"
                                Layout.row:0
                                Layout.column:5
                                Layout.preferredWidth: btnWidth
                                Layout.preferredHeight: btnHeight
                                // 绘制一条边框
                                Shape {
                                    anchors.fill: parent
                                    ShapePath {
                                        strokeWidth: 3
                                        strokeColor: "white"
                                        strokeStyle: ShapePath.SolidLine
                                        startX: 0
                                        startY: recMaxRange.height
                                        PathLine {
                                            x: recMaxRange.width
                                            y: recMaxRange.height
                                        }
                                    }
                                }
                                TextInput{
                                    id:maxRange
                                    anchors.fill: parent
                                    //echoMode: TextInput.Password
                                    text:mymodbus.maxRange
                                    color:"white"
                                    font.pixelSize: 25
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignBottom
                                    MouseArea {
                                           anchors.fill: parent
                                           onClicked: {
                                               // miniKeyBoard.lineValue = "";
                                               // miniKeyBoard.textInput=oldPwd;
                                               //rectangle.selectedView=16
                                               enterNumberPanel.lastButtonIndex = 4;
                                               enterNumberPanel.lastViewIndex =8;
                                               enterNumberPanel.minimum=0;
                                               enterNumberPanel.clearText()
                                               //enterNumberPanel.maximum
                                               enterNumberPanel.itemName=qsTr("最大量程")
                                               enterNumberPanel.itemValue=maxRange.text
                                               enterNumberPanel.textInput=maxRange;
                                               enterNumberPanel.visible=true;
                                               enterNumberPanel.lastRec=mainRec
                                               mainRec.visible=false
                                           }
                                       }

                                }
                            }

        }

    }

    EnterNumberPanel{
          id:enterNumberPanel
          width: 800
          height: 391
          visible:false
    }

    Dialog {
        anchors.centerIn: parent
        id: paraSaveOKDialog
        //title: "提示"
        standardButtons: Dialog.Ok
        //title:qsTr("保存成功！")
        modal: true
        closePolicy: "CloseOnEscape"
//        onAccepted: {

//        }
        z:100
        //onRejected: console.log("Cancel clicked")
    }
}


